<?php
/**
 * Created by PhpStorm.
 * User: 郑合达
 * Date: 2018/12/20
 * Time: 15:31
 */

namespace app\admin\controller;
use app\Model\ActivityModel;
use app\Model\ActivityRebateModel;
use app\Model\CouponModel;
use app\Model\UsersModel;
use app\Model\CouponListModel;
//活动
class Activity extends Comment
{

    /*
    * 优惠券类型列表rebate
    */
     public function index(){
         $data['data']=ActivityModel::paginate(null,false,['query'=>request()->param()]);
         $this->getConAndAct('counp');
         return view('index',$data);
     }
    /*
   * 新增/编辑页面
   */
    public function add()
    {
        //返利
        $getRebate = ActivityRebateModel::order('recharge','desc')->column('*','id');
        $data['rebate'] = $getRebate;
//        dump($getRebate);die();
        $getData = input('get.');
        if ($getData){//编辑
            $getAtivity = ActivityModel::find($getData['id']);//获取该活动信息
            $getAtivity['rebate_id']=explode(',',$getAtivity['rebate_id']);
            $data['data']=$getAtivity;
        }
        return view('add',$data);//页面
    }
    /*
     * 新增/编辑管理员
     */
    public function addData()
    {
        $postData = input('post.');
        $ActivityTime=explode(" - ", $postData['activity']);//时间分割成[0]第一个 [1]第二个 取中间值
        $postData['start_time']=strtotime($ActivityTime[0]);
        $postData['end_time']=strtotime($ActivityTime[1])+86399;
        unset($postData['activity']);
        $postData['rebate_id']=implode(',',$postData['rebate_id']);
        $postData['edittime']=time();//添加/编辑时间
        if (isset($postData['id'])){
            sole('tp_activity',['title'=>$postData['title']],['id',$postData['id']],'活动');//判断修改活动名称是否存在
            $setAdmin = ActivityModel::update($postData,['id'=>$postData['id']]);//编辑
        }else{
            sole('tp_activity',['title'=>$postData['title']],null,'活动');//判断活动名称是否存在
            $setAdmin = ActivityModel::insert($postData);//新增
        }
        return setDatabase($setAdmin);
    }
    /*
     * 删除活动
     */
    public function del(){
        $postData = input('post.');
        $delRole = ActivityModel::destroy($postData['id']);
        return setDatabase($delRole);
    }

    /*
     * 充值返利列表
     */
    public function rebateindex()
    {
        $data['data']=ActivityRebateModel::order('recharge','desc')->paginate(null,false,['query'=>request()->param()]);
        $this->getConAndAct('counp');
        return view('rebateindex',$data);
    }
    /*
      * 新增/编辑返利
      */
    public function rebateadd()
    {
        $getData = input('post.');
        if ($getData['recharge']==''||$getData['recharge']==0){return retJson('',201,'请填写充值金额');}
        if ($getData['rebate']==''||$getData['rebate']==0){return retJson('',201,'请填写返利金额');}
        if ($getData['id']!=0){
            $setAdmin = ActivityRebateModel::update($getData);//编辑
        }else{
            unset($getData['id']);
            $setAdmin = ActivityRebateModel::insert($getData);//新增
        }
        return setDatabase($setAdmin);
    }
    /*
       * 删除
       */
    public function rebatedel(){
        $postData = input('post.');
        $delRole = ActivityRebateModel::destroy($postData['id']);
        return setDatabase($delRole);
    }
}